$(document).ready(function () {
    $(".txt:[type='text']").bind('focus', function () {
        $(this).removeClass('txt');
        $(this).addClass('input_select');
    });
    $(".txt:[type='text']").bind('blur', function () {
        $(this).removeClass('input_select');
        $(this).addClass('txt');
        $('#erro_msg').html('');
    });
    $("#demo.txt").bind('focus', function () {
        $(this).removeClass();
        $(this).addClass('input_select');
    });
    $("#demo.txt").bind('blur', function () {
        $(this).removeClass();
        $(this).addClass('txt');
        $('#erro_msg').html('');
    });
});
function freeZhaoBiao() {
    try {
        var chenghu = ($("#chenghu").length > 0) ? $("#chenghu").val() : '';
        var phone = ($("#phone").length > 0) ? $("#phone").val() : '';
        var province = ($("#province").length > 0) ? $("#province").val() : '';
        var city = ($("#city").length > 0) ? $("#city").val() : '';
        var area = ($("#area").length > 0) ? $("#area").val() : '';
        var xiaoqu = ($("#xiaoqu").length > 0) ? ($("#xiaoqu").val() != '小区' ? $("#xiaoqu").val() : '') : '';
        var mianji = ($("#mianji").length > 0) ? ($("#mianji").val() != '面积' ? $("#mianji").val() : '') : '';
        var decoration = ($("#decoration").length > 0) ? $("#decoration").val() : '';
        var budget = ($("#budget").length > 0) ? $("#budget").val() : '';
        var demo = ($("#demo").length > 0) ? $("#demo").val() : '';
        var type = ($("#type").length > 0) ? $("#type").val() : '';
        var position = ($("#position").length > 0) ? $("#position").val() : '';
        var returntype = ($("#returntype").length > 0) ? $("#returntype").val() : '';
        var istip = ($("#erro_msg").length > 0) ? 1 : 0;
        var regsource = ($("#regsource").length > 0) ? $("#regsource").val() : '';
        var decoType = ($("[name='decoType']").length > 0) ? $("[name='decoType']:checked").val() : '';
    //	var sourceid = jq("#sourceid")[0].value;
    //	var s_sourceid = jq("#s_sourceid")[0].value;
    } catch (e) {

    }
    $.ajax({
        type: "POST",
        url: "?controller=ajax/baoming&controllerAction=save",
        data: "type=" + type + "&phone=" + phone + "&chenghu=" + chenghu + "&province=" + province + "&city=" + city + "&area=" + area + "&xiaoqu=" + xiaoqu + "&mianji=" + mianji + "&demo=" + demo + "&decoration=" + decoration + "&budget=" + budget + "&returntype=" + returntype + "&position=" + position + "&regsource=" + regsource + "&decoType=" + decoType,
        beforeSend: function () {

            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if ((!chenghu && $("#chenghu").length > 0) || chenghu == '请填写您的称呼') {
                $("#chenghu").focus();
                if (istip) {
                    $('#erro_msg').html('请填写您的称呼');
                } else {
                    showInfo("请填写您的称呼");
                }

                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                $("#phone").focus();
                if (istip) {
                    $('#erro_msg').html('请填写您的联系电话');
                } else {
                    showInfo("请填写您的联系电话");
                }

                return false;
            }
        },
        success: function (result) {
            if (result.code == 1) {
                $("#commonbox").hide();
                $("#success_baoming").show();
                $("form").each(function () {
                    this.reset();
                });
                if (result.msg) {
                    if (result.type == 'alert') {
                        showInfo(result.msg);
                    } else {
                        $('#success_msg').html(result.msg);
                    }
                    if ($("#xiaoqu").length <= 0 && result.href) {
                        location.href = result.href;
                    }
                } else {
                    $('#erro_msg').html('');
                    $('#success_msg').html('');
                }
            } else {
                if (res['msg']) {
                    if (res['type'] == 'alert') {
                        showInfo(result.msg);
                    } else {
                        $('#erro_msg').html(result.msg);
                    }
                }
            }
        }
    })
}

function baoming(obj) {
    var _chenghu = $(obj + " [name='chenghu']");
    var chenghu = (_chenghu.length > 0) ? _chenghu.val() : '';
    var _phone = $(obj + " [name='phone']");
    var phone = (_phone.length > 0) ? $.trim(_phone.val()) : '';
    var _province = $(obj + " [name='province']");
    var province = (_province.length > 0) ? _province.val() : '';
    var _city = $(obj + " [name='city']");
    var city = (_city.length > 0) ? _city.val() : '';
    var _area = $(obj + " [name='area']");
    var area = (_area.length > 0) ? _area.val() : '';
    var _xiaoqu = $(obj + " [name='xiaoqu']");
    var xiaoqu = (_xiaoqu.length > 0) ? (_xiaoqu.val() != '小区' ? _xiaoqu.val() : '') : '';
    var _mianji = $(obj + " [name='mianji']");
    var mianji = (_mianji.length > 0) ? (_mianji.val() != '面积' ? _mianji.val() : '') : '';
    var _decoration = $(obj + " [name='decoration']");
    var decoration = (_decoration.length > 0) ? _decoration.val() : '';
    var _budget = $(obj + " [name='budget']");
    var budget = (_budget.length > 0) ? _budget.val() : '';
    var _demo = $(obj + " [name='demo']");
    var demo = (_demo.length > 0) ? _demo.val() : '';
    var _type = $(obj + " [name='type']");
    var type = (_type.length > 0) ? _type.val() : '';
    var _position = $(obj + " [name='position']");
    var position = (_position.length > 0) ? _position.val() : '';
    var _decoType = $(obj + " [name='decoType']");
    var decoType = ($("[name='decoType']").length > 0) ? $("[name='decoType']:checked").val() : '';
    if(!chenghu){
        chenghu= $(obj+" [name='yourname']").length > 0 ? $(obj+" [name='yourname']").val():'';
    }
    if(!phone){
        phone =$(obj+" [name='yourphone']").length > 0 ? $(obj+" [name='yourphone']").val():'';
    }

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "?controller=ajax/baoming&controllerAction=save",
        data: "type=" + type + "&phone=" + phone + "&chenghu=" + chenghu + "&province=" + province + "&city=" + city + "&area=" + area + "&xiaoqu=" + xiaoqu + "&mianji=" + mianji + "&demo=" + demo + "&decoration=" + decoration + "&budget=" + budget + "&position=" + position + "&decoType=" + decoType,
        beforeSend: function () {
            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if ((!chenghu && _chenghu.length > 0) || chenghu == '请填写您的称呼') {
                showInfo('请填写您的称呼', function () {
                    _chenghu.focus();
                });
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {

                showInfo('请填写您的联系电话', function () {
                    _phone.focus();
                });
                return false;
            }
        },
        success: function (result) {
            if (result.code == 1) {
                $("form").each(function () {
                    this.reset();
                });

            }

            if (result.msg) {
                showInfo(result.msg, function () {
                    if (_xiaoqu.length <= 0 && result.href) {
                        /*location.href = result.href;*/
                        window.open(result.href);
                    }
                });
                return false;
            }
        }
    })
}
//招标报名
function simplebaoming(obj) {
    try {
        var _chenghu = $(obj + " [name='chenghu']");
        var _phone = $(obj + " [name='phone']");
        var _type = $(obj + " [name='type']");
        var _position = $(obj + " [name='position']");
        var chenghu = (_chenghu.length > 0) ? _chenghu.val() : '';
        var phone = (_phone.length > 0) ? $.trim(_phone.val()) : '';
        var type = (_type.length > 0) ? $(obj + " [name='type']:checked").val() : '';
        var position = (_position.length > 0) ? _position.val() : '';
    } catch (e) {

    }
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "?controller=ajax/baoming&controllerAction=simple",
        data: "type=" + type + "&phone=" + phone + "&chenghu=" + chenghu + "&position=" + position,
        beforeSend: function () {
            showDialog("loading", "正在读取数据...");
            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if (!chenghu && _chenghu.length > 0) {
                showInfo('请填写您的称呼!', function () {
                    _chenghu.focus();
                });
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                showInfo('请正确填写您的手机号码!', function () {
                    _phone.focus();
                });
                return false;
            }
        },
        success: function (result) {
            if (result.code == 1) {
                $("form").each(function () {
                    //                    this.reset();
                    });
                if (result.msg) {
                    showInfo(result.msg,function(){
                        showWindow('详细需求','/?controller=baoming/index&id='+result['id']+'&type='+result['type'],730);
                    },5)
                }
            }else{
                showInfo(result.msg);
            }
        }
    })
}

function alertbaoming(obj) {
    try {
        var _chenghu = $(obj + " [name='chenghu']");
        var _phone = $(obj + " [name='phone']");
        var _type = $(obj + " [name='type']");
        var _position = $(obj + " [name='position']");

        var chenghu = (_chenghu.length > 0) ? _chenghu.val() : '';
        var phone = (_phone.length > 0) ? _phone.val() : '';
        var type = (_type.length > 0) ? _type.val() : '';
        var position = (_position.length > 0) ? _position.val() : '';
    } catch (e) {

    }
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "?controller=ajax/baoming&controllerAction=save",
        data: "type=" + type + "&phone=" + phone + "&chenghu=" + chenghu + "&position=" + position,
        beforeSend: function () {
            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if (!chenghu && _chenghu.length > 0) {
                alert('请填写您的称呼');
                _chenghu.focus();
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                alert('请填写您的联系电话');
                _phone.focus();
                return false;
            }
            $(obj+':submit').val('正在申请');
        },
        success: function (result) {
            if (result.code == 1) {
                $("form").each(function () {
                    this.reset();
                });
                if (result.msg) {
                    showInfo(result.msg);
                }
            }
        }
    })
}
/**
 * 申请保障金
 */
function freeBaoZhang() {
    try {
        var chenghu = ($("#chenghu").length > 0) ? $("#chenghu").val() : '';
        var phone = ($("#phone").length > 0) ? $("#phone").val() : '';
        var province = ($("#province").length > 0) ? $("#province").val() : '';
        var city = ($("#city").length > 0) ? $("#city").val() : '';
        var area = ($("#area").length > 0) ? $("#area").val() : '';
        var shop_id = ($("#shop_id").length > 0) ? $("#shop_id").val() : '';
        var other = ($("#other").length > 0) ? $("#other").val() : '';
        var issuer = ($("#issuer").attr("checked")) ? $("#issuer").attr("checked") : '';
    } catch (e) {

    }
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "?controller=ajax/baozhang&controllerAction=save",
        data: "phone=" + phone + "&chenghu=" + chenghu + "&province=" + province + "&city=" + city + "&area=" + area + "&shop_id=" + shop_id + "&other=" + other + "&issuer=" + issuer,
        beforeSend: function () {

            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if (!chenghu) {
                $('#erro_msg').html('请填写您的姓名');
                $("#chenghu").focus();
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                $('#erro_msg').html('请填写您的联系电话');
                $("#phone").focus();
                return false;
            }
            if (!issuer) {
                $('#erro_msg').html('您是否已确认信息无误')
                $("#issuer").focus();
                setTimeout(function () {
                    $('#erro_msg').html('');
                }, 3000);
                return false;
            }
        },
        success: function (result) {
            var res = result;
            if (res['code'] == 1) {
                $("form").each(function () {
                    this.reset();
                });
                if (res['msg']) {
                    if (res['type'] == 'alert') {
                        showInfo(res['msg']);
                    } else {
                        $('#success_msg').html(res['msg']);
                    }
                } else {
                    $('#erro_msg').html('');
                    $('#success_msg').html('');
                }
            } else {
                if (res['msg']) {
                    $('#erro_msg').html(res['msg']);
                }

            }
        }
    })
}

function simplebzj(obj) {
    try {
        var _chenghu = $(obj + " [name='chenghu']");
        var _phone = $(obj + " [name='phone']");

        var chenghu = (_chenghu.length > 0) ? _chenghu.val() : '';
        var phone = (_phone.length > 0) ? _phone.val() : '';
    } catch (e) {

    }
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "?controller=ajax/baozhang&controllerAction=save",
        data: "phone=" + phone + "&chenghu=" + chenghu,
        beforeSend: function () {

            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if (!chenghu) {
                showInfo('请填写您的称呼', function () {
                    _chenghu.focus();
                });
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                showInfo('请填写您的联系电话', function () {
                    _phone.focus();
                });
                return false;
            }
        },
        success: function (result) {
            if (result.code == 1) {
                $("form").each(function () {
                    this.reset();
                });
            }
            if (result.msg) {
                showInfo(result.msg);
            }
        }
    })
}

/**
 * 建材招标快速报名
 * @param obj
 */
function simplejczb(obj) {
    try {
        var _chenghu = $(obj + " [name='chenghu']");
        var _phone = $(obj + " [name='phone']");
        var chenghu = (_chenghu.length > 0) ? _chenghu.val() : '';
        var phone = (_phone.length > 0) ? _phone.val() : '';
    } catch (e) {

    }
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/?controller=jczb/jczbPublish&controllerAction=fastPublish",
        data: "phone=" + phone + "&chenghu=" + chenghu,
        beforeSend: function () {
            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if (!chenghu) {
                showInfo('请填写您的称呼', function () {
                    _chenghu.focus();
                });
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                showInfo('请填写您的联系电话', function () {
                    _phone.focus();
                });
                return false;
            }
        },
        success: function (result) {
            var data = result.data
            if (result.code == 'successful') {
                showInfo(result.message, function () {
                    window.open(data.url);
                });
            } else {
                showInfo(result.message);
            }
        }
    })
}

/**
 * 装修团购,建材团购,咨询首页,预约样板房
 * @param obj
 */
function freezixun(obj) {
    try {
        var chenghuID = $(obj + " [name='chenghu']");
        var chenghu = ($(chenghuID).length > 0) ? $(chenghuID).val() : '';
        var phoneID = $(obj + " [name='phone']");
        var phone = ($(phoneID).length > 0) ? $(phoneID).val() : '';
        var _xiaoqu = $(obj + " [name='xiaoqu']");
        var xiaoqu = (_xiaoqu.length > 0) ? _xiaoqu.val() : '';
        var _mianji = $(obj + " [name='mianji']");
        var mianji = (_mianji.length > 0) ? _mianji.val() : '';
        var _decoration = $(obj + " [name='decoration']");
        var decoration = (_decoration.length > 0) ? _decoration.val() : '';
        var _decoType = $(obj + " [name='decoType']");
        var decoType = (_decoType.length > 0) ? _decoType.val() : '';
        var _hx = $(obj + " [name='hx']");
        var hx = (_hx.length > 0) ? _hx.val() : '';
        var _budget = $(obj + " [name='budget']");
        var budget = (_budget.length > 0) ? _budget.val() : '';
        var _demo = $(obj + " [name='demo']");
        var demo = (_demo.length > 0) ? _demo.val() : '';
        var _companyID = $(obj + " [name='companyID']");
        var companyID = (_companyID.length > 0) ? _companyID.val() : '';
        var _designerID = $(obj + " [name='designerID']");
        var designerID = (_designerID.length > 0) ? _designerID.val() : '';
        var _ybfID = $(obj + " [name='ybfID']");
        var ybfID = (_ybfID.length > 0) ? _ybfID.val() : '';
        var _type = $(obj + " [name='type']");
        var type = (_type.length > 0) ? _type.val() : '';
        var _position = $(obj + " [name='position']");
        var position=(_position.length>0)?_position.val():'';
    } catch (e) {}

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "?controller=ajax/zixun&controllerAction=save",
        data: "type=" + type + "&phone=" + phone + "&ybfID=" + ybfID + "&chenghu=" + chenghu + "&xiaoqu=" + xiaoqu + "&mianji=" + mianji + "&demo=" + demo + "&decoration=" + decoration + "&budget=" + budget + "&designerID=" + designerID + "&companyID=" + companyID+ "&decoType=" + decoType+ "&hx=" + hx+'&position='+position,
        beforeSend: function () {
            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if (!chenghu) {
                alert('请填写您的称呼');
                $(chenghuID).focus();
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                alert('请填写您的联系电话');
                $(phoneID).focus();
                return false;
            }
        },
        success: function (result) {
            showInfo(result.msg);
            $(obj).each(function () {
                this.reset();
            });
        }
    })
}

/**
 * http://xm.xtuan.com/4120/yhq28051.html
 * 优惠券下载 用到1次
 * @param obj
 */
function freeydowncoupon(obj) {
    var _chenghu = $(obj + " [name='chenghu']");
    var chenghu = ($(_chenghu).length > 0) ? $(_chenghu).val() : '';
    var _phone = $(obj + " [name='phone']");
    var phone = ($(_phone).length > 0) ? $(_phone).val() : '';
    var _ID = $(obj + " [name='ID']");
    var ID = ($(_ID).length > 0) ? $(_ID).val() : '';
    var _companyID = $(obj + " [name='companyID']");
    var companyID = ($(_companyID).length > 0) ? $(_companyID).val() : '';
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/?controller=ajax/downcoupon&controllerAction=save",
        data: "phone=" + phone + "&chenghu=" + chenghu + "&ID=" + ID + "&companyID=" + companyID,
        beforeSend: function () {
            var reg1 = /^((\(\d{2,3}\))|(\d{3}\-))?(13|15|18)\d{9}$/;
            if (!chenghu) {
                alert('请填写您的称呼');
                $("#chenghu").focus();
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                alert('请填写您的联系电话');
                $("#phone").focus();
                return false;
            }
        },
        success: function (result) {
            showInfo(result.message);
        }
    })
}

/**
 * 检测是否为手机号码
 * @param str
 * @returns {boolean}
 */
function isTel(str) {
    var reg = /^13\d{9,9}$/;
    var reg2 = /^15\d{9,9}$/;
    var reg3 = /^18\d{9,9}$/;
    var my = false;
    if (reg.test(str))my = true;
    if (reg2.test(str))my = true;
    if (reg3.test(str))my = true;
    return my;
}

/**
 * http://www.xtuan.com/zhaobiao/perfect-15805-18805543210/
 * 完善招标,用到一次
 */
function perfectzhaobiao() {
    try {
        var chenghu = ($("#chenghu").length > 0) ? $("#chenghu").val() : '';
        var phone = ($("#phone").length > 0) ? $("#phone").val() : '';
        var province = ($("#province").length > 0) ? $("#province").val() : '';
        var city = ($("#city").length > 0) ? $("#city").val() : '';
        var area = ($("#area").length > 0) ? $("#area").val() : '';
        var xiaoqu = ($("#xiaoqu").length > 0) ? $("#xiaoqu").val() : '';
        var mianji = ($("#mianji").length > 0) ? $("#mianji").val() : '';
        var decoration = $("[name = 'decoration']:checked").val();
        var budget = $("[name = 'budget']:checked").val();
        var demo = ($("#demo").length > 0) ? $("#demo").val() : '';
        var bidID = ($("#bidID").length > 0) ? $("#bidID").val() : '';
        var zx = $("[name = 'zx']:checked").val();
        var email = ($("#email").length > 0) ? $("#email").val() : '';
        var password = ($("#password").length > 0) ? $("#password").val() : '';
        var repassword = ($("#repassword").length > 0) ? $("#repassword").val() : '';
    } catch (e) {

    }
    $.ajax({
        type: "POST",
        url: "?controller=ajax/perfectbid&controllerAction=save",
        data: "phone=" + phone + "&chenghu=" + chenghu + "&province=" + province + "&city=" + city + "&area=" + area + "&xiaoqu=" + xiaoqu + "&mianji=" + mianji + "&bidID=" + bidID + "&decoration=" + decoration + "&budget=" + budget + "&zx=" + zx + "&email=" + email + "&password=" + password + "&repassword=" + repassword + "&demo=" + demo,
        dataType: "json",
        beforeSend: function () {
            if (!email && $("#email").length > 0) {
                $("#email").focus();
                showInfo("请填写您的注册邮箱");
                return false;
            }
            if (!password && $("#password").length > 0) {
                $("#password").focus();
                showInfo("请填写您的注册密码");
                return false;
            }
            if (password != repassword && $("#password").length > 0 && $("#repassword").length > 0) {
                $("#password").focus();
                showInfo("两次密码不一致");
                return false;
            }
            var reg1 = /^1(3|4|5|7|8)\d{9}$/;
            if (!chenghu && $("#chenghu").length > 0) {
                $("#chenghu").focus();
                showInfo("请填写您的姓名");
                return false;
            }
            else if (!isTel(phone) && !reg1.test(phone)) {
                $("#phone").focus();
                showInfo("请填写您的联系电话");
                return false;
            }
            if (!mianji && $("#mianji").length > 0) {
                $("#mianji").focus();
                showInfo("请填写您的面积");
                return false;
            }
        },
        success: function (result) {
            showInfo(result.message, function () {
                if (result.code == 'successful') {
                    location.href = result.data.url;
                }
            });
        }
    })
}



